package com.kunbo.cn.controller.system.user;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kunbo.cn.entity.system.Menu;
import com.kunbo.cn.entity.system.user.Position;
import com.kunbo.cn.service.system.user.PositionService;
import com.kunbo.cn.util.json.Json;
import com.kunbo.cn.util.retu.ReturnBean;

@Controller  
@RequestMapping("/system/user/position/*")  
public class PositionController {
	
	@Resource
	private PositionService positionService;
    
	@RequestMapping(value="getAll.do")  
    public @ResponseBody List<Position> getAll(HttpServletRequest request,HttpServletResponse response,HttpSession session) {
    		return positionService.list();
    }
    
    @RequestMapping(value="save.do")  
    public @ResponseBody ReturnBean save(HttpServletRequest request,HttpServletResponse response,HttpSession session) {
    		String data = request.getParameter("data").replaceAll("\"\"", "null");
    		Position position = (Position) Json.toObject(Position.class, data);
    		positionService.save(position);
    		return new ReturnBean(true,"权限配置成功");
    }
    
    @RequestMapping(value="update.do")  
    public @ResponseBody ReturnBean update(HttpServletRequest request,HttpServletResponse response,HttpSession session) {
    		String data = request.getParameter("data").replaceAll("\"\"", "null");
    		Position position = (Position) Json.toObject(Position.class, data);
    		positionService.update(position);
    		return new ReturnBean(true,"");
    }    
    
    @RequestMapping(value="delete.do")  
    public @ResponseBody ReturnBean delete(HttpServletRequest request,HttpServletResponse response,HttpSession session) {
    		String data = request.getParameter("data").replaceAll("\"\"", "null");
    		Position position = (Position) Json.toObject(Position.class, data);
    		positionService.delete(position);
    		return new ReturnBean(true,"");
    }
    
    @RequestMapping(value="findById.do")  
    public @ResponseBody Position findById(HttpServletRequest request,HttpServletResponse response,HttpSession session) {
    		String id = request.getParameter("id");
    		return positionService.findById(Integer.parseInt(id));
    }
    //角色配置权限
    @RequestMapping(value="authorityConfigure.do")  
    public @ResponseBody ReturnBean authorityConfigure(HttpServletRequest request,HttpServletResponse response,HttpSession session) {
    		String jsonStr = request.getParameter("jsonStr");
    		try {
				positionService.authorityConfigure(jsonStr);
				return new ReturnBean(true,"配置成功");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return new ReturnBean(false,"异常配置失败");
			}
    }
    
    @RequestMapping(value="getMenuByPositionId.do")  
    public @ResponseBody List<Menu> getMenuByPositionId(HttpServletRequest request,HttpServletResponse response,HttpSession session) {
    		String id = request.getParameter("id");
    		return positionService.getMenuByPositionId(Integer.parseInt(id));
    }
	    
}
